using Mapster;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using QiAdmin.Core;
using QiAdmin.Core.Services.Log;
using QiAdmin.Core.Services.Token;
using QiAdmin.Entity;
using QiAdmin.Model.Dtos;
using QiAdmin.Model.Dtos.Input;
using QiAdmin.Model.Dtos.Output;
using QiAdmin.Repository.Repository;
using QiAdmin.Service.IService;
using ICacheService = QiAdmin.Core.Services.Cache.ICacheService;

namespace QiAdmin.Service.Service
{
    /// <summary>
    /// TSysNotice Services
    /// </summary>
    public class TSysNoticeService : BaseService<TSysNotice, TSysNotice_IN, TSysNotice_OUT>, ITSysNoticeService
    {
        readonly TSysNoticeRepository _tSysNoticeRepository;
        public TSysNoticeService(ILogService logService,
            ITokenService tokenService, ICacheService cacheService, IHttpContextAccessor httpContextAccessor, TSysNoticeRepository tSysNoticeRepository) : base(logService, tokenService, cacheService, httpContextAccessor.HttpContext)
        {
            _tSysNoticeRepository= tSysNoticeRepository;
        }

        #region 增删改查-自动生成

        /// <summary>
        /// 创建默认条件
        /// </summary>
        /// <returns></returns>
        public Expressionable<TSysNotice> CreateExp()
        {
            var exp = Expressionable.Create<TSysNotice>();

            return exp;
        }

        /// <summary>
        /// 新增单条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult Add(TSysNotice_IN input)
        {
            var model = input.Adapt<TSysNotice>();
            int Id = _tSysNoticeRepository.InsertReturnIdentity(model);
            return CommonResult.Convert(Id > 0);
        }

        /// <summary>
        /// 新增多条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult AddRange(List<TSysNotice_IN> input)
        {
            var list = input.Adapt<List<TSysNotice>>();
            var result = _tSysNoticeRepository.InsertRange(list);
            return CommonResult.Convert(result);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult Edit(TSysNotice_IN input)
        {
            var exp = CreateExp();
            exp.And(t => t.Id == input.Id);
            var model = input.Adapt<TSysNotice>();
            var result = _tSysNoticeRepository.UpdateAndIgnore(model, exp.ToExpression());
            return CommonResult.Convert(result);
        }

        /// <summary>
        /// 获取单条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult<TSysNotice_OUT> Get(TSysNotice_IN input)
        {
            var exp = CreateExp();
            exp.And(t => t.Id == input.Id);

            var result = _tSysNoticeRepository.GetFirst(exp.ToExpression()).Adapt<TSysNotice_OUT>();
            return new CommonResult<TSysNotice_OUT>(result);
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult<PageOutput<TSysNotice_OUT>> GetPageList(TSysNotice_IN input)
        {
            var exp = CreateExp();

            exp.AndIF(input.StartQueryTime != DateTime.MinValue, t => t.SysCreateTime >= input.StartQueryTime.Date);
            exp.AndIF(input.EndQueryTime != DateTime.MinValue, t => t.SysCreateTime < input.EndQueryTime.Date.AddDays(1));
            exp.AndIF(!string.IsNullOrEmpty(input.Type), t => t.Type == input.Type);
            exp.AndIF(input.Status!=null, t => t.Status == input.Status);
            exp.AndIF(!string.IsNullOrEmpty(input.Title), t => t.Title.Contains(input.Title));

            PageOutput<TSysNotice_OUT> pageOutput = new PageOutput<TSysNotice_OUT>();
            PageModel pageModel = new() { PageIndex = input.PageIndex, PageSize = input.PageSize };
            pageOutput.Data = _tSysNoticeRepository.GetPageList(exp.ToExpression(), pageModel,t=>t.Id,OrderByType.Desc).Adapt<List<TSysNotice_OUT>>();
            pageOutput.DataCount = pageModel.TotalCount;

            return new CommonResult<PageOutput<TSysNotice_OUT>>(pageOutput);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult Remove(TSysNotice_IN input)
        {
            var exp = CreateExp();
            exp.And(t => t.Id == input.Id);

            var result = _tSysNoticeRepository.Delete(exp.ToExpression());
            return CommonResult.Convert(result);
        }

        /// <summary>
        /// 删除多条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult RemoveRange(List<TSysNotice_IN> input)
        {
            var exp = CreateExp();
            exp.And(t => input.Select(s => s.Id).Contains(t.Id));

            var result = _tSysNoticeRepository.Delete(exp.ToExpression());
            return CommonResult.Convert(result);
        }

        #endregion

    }
}
